import modal from './modal.vue'
export default {
    install(vue) {
        let modalContent = vue.extend(modal)
        let caches = null

        function getInstance(Component) {
            if (!caches) {
                caches = new modalContent()
                document.body.appendChild(caches.$mount().$el)
            }
            return caches
        }
        vue.prototype.$alert = (obj) => {
            let action = {}
            let alertObj = getInstance()
            alertObj.okCallback = () => {
                action.resolve({
                    ...arguments
                })
            }
            alertObj.modalShow({
                type: 'alert',
                ...obj
            })
            return new Promise((resolve, reject) => {
                action = {
                    resolve,
                    reject
                }
            })
        }
        vue.prototype.$toast = (obj) => {
            let toastObj = getInstance()
            toastObj.modalShow({
                type: 'toast',
                ...obj
            })
        }
        vue.prototype.$loading = (obj) => {
            let loadingObj = getInstance()
            loadingObj.modalShow({
                type: 'loading',
                ...obj
            })
        }
        vue.prototype.$hideloading = (obj) => {
            let loadingObj = getInstance()
            loadingObj.modalHide()
        }
        vue.prototype.$confirm = (obj, ok, cancel) => {
            let action = {}
            let confirmObj = getInstance()
            confirmObj.modalShow({
                type: 'confirm',
                ...obj
            })
            confirmObj.okCallback = () => {
                action.resolve({
                    ...arguments
                })
            }
            confirmObj.onCancelCallback = () => {
                action.reject({
                    ...arguments
                })
            }
            return new Promise((resolve, reject) => {
                action = {
                    resolve,
                    reject
                }
            })
        }

    }
}